package com.zdsoft.finance.organization.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.organization.entity.Bank;
import com.zdsoft.finance.organization.repository.BankRepository;
import com.zdsoft.finance.organization.service.BankService;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 银行
 * @author gufeng
 * @date 2016-11-16
 */
@Service
public class BankServiceImpl extends BaseServiceImpl<Bank, CustomRepository<Bank, String>>
implements BankService {

	@Autowired
	private BankRepository bankRepository;
	
	@Override
	@Transactional(rollbackFor = BusinessException.class)
	public Bank saveOrUpdate(Bank po) throws BusinessException {
		Bank bean = null;
		if(ObjectHelper.isNotEmpty(po.getParent_id())){
			//保存子
			Bank parent = bankRepository.findOne(po.getParent_id());
			po.setId(null);
			po.setParent(parent);
			//复用父级数据
			po.setOrgCode(parent.getOrgCode());
			po.setPledgeRate(parent.getPledgeRate());
			po.setLossShareRate(parent.getLossShareRate());
			po.setCreditAmount(parent.getCreditAmount());
			po.setMagnification(parent.getMagnification());
			po.setRateLimiting(parent.getRateLimiting());
			po.setCompFreePeriod(parent.getCompFreePeriod());
			po.setDeadlineUnit(parent.getDeadlineUnit());
			po.setDeadlineUnitName(parent.getDeadlineUnitName());
			po.setCreditBeginDate(parent.getCreditBeginDate());
			po.setCreditEndDate(parent.getCreditEndDate());
			po.setBankOutletsCode(parent.getBankOutletsCode());
			bean = bankRepository.saveEntity(po);
			parent.getChildren().add(bean);
			bankRepository.updateEntity(parent);
		}else{
			//普通保存
			if(ObjectHelper.isNotEmpty(po.getId())){
				bean = bankRepository.findOne(po.getId());
			}
			if(ObjectHelper.isNotEmpty(bean)){
				//修改
				bean.setOrgType(po.getOrgType());
			    bean.setOrgName(po.getOrgName());
			    bean.setOrgFullName(po.getOrgFullName());
			    bean.setOrgCode(po.getOrgCode());
			    bean.setContactMan(po.getContactMan());
			    bean.setTelephone(po.getTelephone());
			    bean.setRemark(po.getRemark());
			    bean.setAutoRenew(po.getAutoRenew());
			    bean.setEnable(po.getEnable());
			    bean.setSearchCode(po.getSearchCode());
			    bean.setCooperationAttachment(po.getCooperationAttachment());
			    bean.setCreditBeginDate(po.getCreditBeginDate());
			    bean.setCreditEndDate(po.getCreditEndDate());
			    bean.setCreditAmount(po.getCreditAmount());
			    bean.setCreditBalance(po.getCreditBalance());
			    //bank
			    bean.setPledgeRate(po.getPledgeRate());
			    bean.setCompFreePeriod(po.getCompFreePeriod());
			    bean.setDeadlineUnit(po.getDeadlineUnit());
			    bean.setDeadlineUnitName(po.getDeadlineUnitName());
			    bean.setBankOutletsCode(po.getBankOutletsCode());
			    bean.setMagnification(po.getMagnification());
			    bean.setRateLimiting(po.getRateLimiting());
			    bean.setLossShareRate(po.getLossShareRate());
			    bean.setManagerName(po.getManagerName());
			    bean.setManagerPhone(po.getManagerPhone());
			    bean.setSingleLimit(po.getSingleLimit());
			    bean.setBankNature(po.getBankNature());
			    bean.setProtocolType(po.getProtocolType());
			    bean.setOtherFee(po.getOtherFee());
			    bean = bankRepository.updateEntity(bean);
			}else{
				//添加
				bean = bankRepository.saveEntity(po);
			}
		}
		return bean;
	}

}
